<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of BaseController
 *
 * @author Administrator
 */

namespace frontend\api\controllers;
use yii\web\Controller;
use Yii;
 use frontend\library\MT;
define('CURRENT_TIME', time());
define('CURRENT_TIME_MICRO', microtime());
define('CURRENT_PLATFORM', @ MT::judgeUserAgent());
define('CURRENT_VERSION', MT::judgeUserVersion());


class BaseController  extends Controller {
      const key = "9c6945559796648675a4d80bd1fd65ab";
    //put your code here
    
    
     protected function _sendResponse($data, $message = '') {
    


        $json['code'] = 200;
        $json['message'] = $message;
        $json['data'] = $data;
        // $json['update'] = false;
//        $this->loginUser = UserService::getLoginUser();
//        if ($this->loginUser) {
//            $updateData = MT::getAllData($this->loginUser->uid);
//            foreach ($updateData as $credit => $rate) {
//                if (!isset($this->loginUser->info[$credit]))
//                    continue;
//                $json['update'][$credit] = array('last' => $this->loginUser->info[$credit] + $rate, 'rate' => $rate);
//            }
//
//        }
        if (isset($_GET["callback"])) {
            echo $_GET['callback'] . '(' . CJSON::encode($json) . ')';
        } else {
            echo json_encode($json);
        }

        //Yii::$app->end();
        exit;
    }
    
    
    
    
    /* 获取参数
     * @param string $name
     * @param string $type get|post|cookie
     * @param bool $bindKey
     */

    protected function getInput($name, $type = '', $bindKey = false) {
        if (is_array($name)) {
            $result = array();
            foreach ($name as $key => $value) {
                $_k = $bindKey ? $value : $key;
                $result[$_k] = $this->getInput($value, $type);
            }
            return $result;
        } elseif ($name) {
            $value = '';
            switch (strtolower($type)) {
                case 'get':
                    $value = Yii::$app->request->get($name);
                    // $value = $_GET[$name];
                    break;
                case 'post':
                    // $value = $_POST[$name];
                    $value = Yii::$app->request->post($name);
                    break;
                case 'cookie':
                    // $value = $_COOKIE[$name];
                    $value = Yii::$app->request->getCookies($name);
                    break;
                case 'body':
                    $value = Yii::$app->request->getRawBody($name);
                    break;
                
                default :
                    $value = isset($_REQUEST[$name]) ? $_REQUEST[$name] : '';
            }
            return $value;
        }
        return '';
    }
    
    /*
      检查签名
     */

    protected function checkSignature($field = array(), $signature = '') {
        if (isset($_REQUEST['debug']) && $_REQUEST['debug']) {
            return true;
        }

        $tmpArr = array();
        foreach ($field as $k) {
            if (!isset($_REQUEST[$k])) {
                $this->verifyError(self::$E_PARAM, $k);
            } elseif ($k !== 'signature') {

                if (is_array($_REQUEST[$k])) { //支持数组加密
                    foreach ($_REQUEST[$k] as $k1 => $v1) {
                        $tmpArr [$k . $k1] = $v1;
                    }
                } else {
                    $tmpArr [$k] = $_REQUEST[$k];
                }
            }
        }
        ksort($tmpArr);
        $tmpStr = implode($tmpArr);
        $tmpStr . self::key;

        $tmpStr = md5($tmpStr . self::key);

        $signature = $_REQUEST['signature'];
        if ($tmpStr !== $signature) {
            $this->verifyError(self::$E_SIGNATURE);
        }
        return true;
    }
    
}
